package matse11.lspiel;

import java.io.*;
import java.util.Scanner;

public class HighScore {
	private String [] name = new String[10];
	private int [] zuege = new int[10];
	
	public HighScore(){
		leseHS();
	}
	private void leseHS(){
		File myFile = new File("HighScore.txt");
		String s;
		
		int i=0;
		try {
			Scanner mySc = new Scanner(myFile);
			while(mySc.hasNextLine()){
				s = mySc.nextLine();
				name[i] = s.split(",")[0];
				zuege[i] = Integer.parseInt(s.split(",")[1]);
				System.out.println(name[i]+" , "+zuege[i]);
				i++;
			}	
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	private void schreibeHS(){
		File myFile = new File("Highscore.txt");
		try {
			PrintWriter myPw = new PrintWriter(myFile);
			for (int i = 0; i < name.length; i++) {
				myPw.println(name[i]+","+zuege[i]);
			}
			myPw.close();
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public boolean vergleiche(String name,int zuege){
		boolean best=false;
		if(zuege > this.zuege[9]){
			for (int i = 0; i < this.name.length; i++) {
				if(zuege > this.zuege[i]){
					setzeScore(name, zuege, i);
					schreibeHS();
					best = true;
					break;
				}
			}
		}
		
		return best;
	}
	
	private void setzeScore(String name, int zuege, int index){
		for(int i=9; i>=index;i--)
			if(i!=index){
				this.name[i]=this.name[i-1];
				this.zuege[i]=this.zuege[i-1];
			}
			else{
				this.name[i]=name;
				this.zuege[i]=zuege;
				
			}
	}
}
